package ControllerSrevlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import MYJDBC.jdbc;
import User_index.User_Login;
import User_index.User_dao;


/**
 * Servlet implementation class BlueVideoUserIndex
 */
@WebServlet("/BlueVideoUserIndex")
public class BlueVideoUserIndex extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BlueVideoUserIndex() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		jdbc jc = new jdbc();
		request.setAttribute("SqlUser",jc.getSqlUser());
		request.setAttribute("SqlPass",jc.getSqlPass());
		request.setAttribute("SqlName", jc.getSqlName());
		
		request.getRequestDispatcher("user_index.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String userid = request.getParameter("userid");
		String username = request.getParameter("username");
		String userdress = request.getParameter("userdress");
		String usernumber = request.getParameter("usernumber");
		
		jdbc jc = new jdbc();
		
		try {
			jc.SqlUpdate("update user_index set user_name = '"+username+"',user_dress = '"+userdress+"',user_number = '"+usernumber+"' where user_id = "+userid+"");
			jc.UpdateClose();
			User_dao user = new User_dao();
			User_Login login = new User_Login();
			user = login.UserLogin(usernumber);
			ServletContext context = this.getServletContext();
			context.setAttribute("user", user);
			response.sendRedirect("BlueVideoUserIndex");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}

}
